package rmhc.gui
{
	import com.greensock.TweenLite;
	import com.greensock.TweenMax;
	import com.greensock.plugins.BlurFilterPlugin;
	import com.greensock.plugins.TweenPlugin;
	
	import flash.display.MovieClip;
	import flash.events.Event;

	public class ScreenManager extends MovieClip
	{
		public const TWEEN_TIME:Number = 0.8;
		
		protected var screen:MovieClip , old:MovieClip;
		protected var back:BackScreen;
		
		//protected static var inst:ScreenManager;
		
		public function ScreenManager()
		{
			//inst = this;
			TweenPlugin.activate([BlurFilterPlugin]);
			back = new BackScreen();
			addChild( back );
		}
		
		/*public static function getManager():ScreenManager
		{
			return inst;
		}*/
		
		public function showGreet():GreetScreen
		{
			var s:GreetScreen = new GreetScreen();
			setScreen( s );
			back.setSate( back.SCREEN1 );
			return s;
		}
		public function showAvatar():AvatarScreen
		{
			var s:AvatarScreen = new AvatarScreen();
			setScreen( s );
			back.setSate( back.SCREEN2 );
			return s;
		}
		public function showPost():PostScreen
		{
			var s:PostScreen = new PostScreen();
			setScreen( s );
			back.setSate( back.SCREEN3 );
			return s;
		}
		
		protected function setScreen( newScreen:MovieClip ):void
		{
			hideOld();
			
			screen = newScreen;
			screen.alpha = 0;
			addChild( screen );
			TweenLite.to( screen , TWEEN_TIME , { alpha : 1 } );
		}
		
		protected function hideOld():void
		{
			if ( screen == null )
			{
				return;
			}
			old = screen;
			old.mouseChildren = old.mouseEnabled = false;
			
			var tweenBlur:Object = { blurX:22 , blurY:22 };
			var tw:TweenMax = new TweenMax( old , TWEEN_TIME , { alpha : 0 , blurFilter : tweenBlur } );
			tw.addEventListener( Event.COMPLETE , onRemOld );
		}
		protected function onRemOld( e:Event ):void
		{
			var tw:TweenMax = e.target as TweenMax;
			var targ:MovieClip = tw.target as MovieClip;
			if ( targ == old )
			{
				old = null;
			}
			if ( targ.parent != null )
			{
				targ.parent.removeChild( targ );
			}
		}
	}
}